package com.yeyks.base;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: yyks-admin
 * @description:
 * @author: chenzheng
 * @create: 2019-04-25 10:57
 **/
public class BaseController {
    protected Logger logger = LogManager.getLogger(this.getClass());

    public BaseController() {
    }

    @InitBinder
    public void initBinding(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        //binder.registerCustomEditor(Date.class, new JSpinner.DateEditor());
    }

    protected void write(HttpServletResponse response, String content) {
        try {
            response.getWriter().write(content);
            response.getWriter().flush();
        } catch (IOException var4) {
            this.logger.error(var4);
        }
    }

    /**
     * 获取用户登录Ip
     * @param request
     * @return
     */

     public static String getIpAddr(HttpServletRequest request) {
                String ip = request.getHeader("x-forwarded-for");
                if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                         ip = request.getHeader("Proxy-Client-IP");
                     }
                 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                         ip = request.getHeader("WL-Proxy-Client-IP");
                    }
                 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                      ip = request.getRemoteAddr();
                     }
                 if (ip.equals("0:0:0:0:0:0:0:1")) {
                         ip = "本地";
                   }
                 return ip;
             }
}
